var wxCharts = require('../../utils/wxcharts.js');
var app = getApp();
var columnChart = null;
var lineChart = null;
var startPos = null;
var pieChart = null;
var chartData = {
  main: {
    title: '各个行业就业量',
    data: [99, 20, 45, 37,56],
    categories: ['软件', '金融', '建工', '医药', '政务']
  },
  
};
Page({
  data: {
    chartTitle: '各个行业就业量',
    chartlineTitle: '历年就业率',
    chartpieTitle: '就业比例',
    isMainChartDisplay: true
  },

  touchHandler: function (e) {
    lineChart.scrollStart(e);
  },

  touchHandlerPie: function (e) {
    console.log(pieChart.getCurrentDataIndex(e));
  },

  moveHandler: function (e) {
    lineChart.scroll(e);
  },
  touchEndHandler: function (e) {
    lineChart.scrollEnd(e);
    lineChart.showToolTip(e, {
      format: function (item, category) {
        return category + ' ' + item.name + ':' + item.data
      }
    });
  },
  createSimulationData: function () {
    var categories = [];
    var data = [];
    for (var i = 0; i < 10; i++) {
      categories.push('2019-' + (i + 1));
      data.push(Math.random() * (100 - 10) + 10);
    }
    return {
      categories: categories,
      data: data
    }
  },

  backToMainChart: function () {
    this.setData({
      chartTitle: chartData.main.title,
      isMainChartDisplay: true
    });
    columnChart.updateData({
      categories: chartData.main.categories,
      series: [{
        name: '就业量',
        data: chartData.main.data,
        format: function (val, name) {
          return val.toFixed(2);
        }
      }]
    });
  },
  touchHandler: function (e) {
    var index = columnChart.getCurrentDataIndex(e);
    if (index > -1 && index < chartData.sub.length && this.data.isMainChartDisplay) {
      this.setData({
        chartTitle: chartData.sub[index].title,
        isMainChartDisplay: false
      });
      columnChart.updateData({
        categories: chartData.sub[index].categories,
        series: [{
          name: '就业量',
          data: chartData.sub[index].data,
          format: function (val, name) {
            return val.toFixed(2);
          }
        }]
      });

    }
  },
  onReady: function (e) {
    var windowWidth = 150;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    columnChart = new wxCharts({
      canvasId: 'columnCanvas',
      type: 'column',
      animation: true,
      categories: chartData.main.categories,
      series: [{
        name: '就业量',
        data: chartData.main.data,
        format: function (val, name) {
          return val.toFixed(2);
        }
      }],
      yAxis: {
        format: function (val) {
          return val;
        },
      
        min: 0
      },
      xAxis: {
        disableGrid: false,
        type: 'calibration'
      },
      extra: {
        column: {
          width: 14
        }
      },
      width: windowWidth,
      height: 150,
    });
  },


  onLoad: function (e) {


    var windowWidth = 150;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    var simulationData = this.createSimulationData();
    lineChart = new wxCharts({
      canvasId: 'lineCanvas',
      type: 'line',
      categories: simulationData.categories,
      animation: false,
      series: [{
        name: '就业率',
        data: simulationData.data,
        format: function (val, name) {
          return val.toFixed(2);
        }
      }],
      xAxis: {
        disableGrid: false
      },
      yAxis: {
        
        format: function (val) {
          return val.toFixed(2);
        },
        min: 0
      },
      width: windowWidth,
      height: 150,
      dataLabel: true,
      dataPointShape: true,
      enableScroll: true,
      extra: {
        lineStyle: 'curve',
        width: 14
      }
    });




    var windowWidthPie = 200;
    try {
      var res = wx.getSystemInfoSync();
      windowWidthPie = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    pieChart = new wxCharts({
      animation: true,
      canvasId: 'pieCanvas',
      type: 'pie',
      series: [{
        name: '就业',
        data: 15,
      }, {
        name: '未就业',
        data: 35,
      }, {
        name: '实习',
        data: 78,
      }, {
        name: '考研',
        data: 63,
      }],
      width: windowWidthPie,
      height: 200,
      dataLabel: true,
    });
  }
});